home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS01.ADF
/
C
/
quick.c
< prev
next >
Wrap
C/C++ Source or Header
|
1985-12-29
|
1KB
|
51 lines
#include <stdio.h>
/* quicksort module -- S. Harrison 8/84 */
/* sorts strings pointed to by base[lo] .. base[hi] into increasing order */
quick( lo, hi, base )
int lo, hi;
char *base[];
{
int i, j, piv;
char *pivot;
if( lo < hi ) {
piv = ( lo + hi ) / 2; /* select center pivot */
i = lo;
j = hi;
pivot = base[piv];
while( i < j ) {
while( i < j && strcmp( base[i], pivot ) <= 0 )
i++;
while( j < i && strcmp( base[j], pivot ) >= 0 )
j--;
if( i < j ) {
swap( &base[i], &base[j] );
}
}
if( piv < i && strcmp( base[i], pivot ) > 0 ) i--;
swap( &base[i], base[piv] ); /* put pivot element in */
/* correct "middle" position */
if( i - lo <= hi - i ) {
quick( lo, i - 1, base );
quick( i + 1, hi, base );
} /* do shortest subset first */
else {
quick( i + 1, hi, base );
quick( lo, i - 1, base );
}
}
}
swap( x, y ) /* swap a pair of pointers */
char *x[], *y[];
{
char *temp;
temp = *x;
*x = *y;
*y = temp;
}